---
title: "N170_misc_mkd"
author: "Anonymized For Peer-Review"
date: "4/27/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
NAME <- '2_Calculating_epsilon_means' ## Name of the R file goes here
PROJECT <- 'Megagrant_2016_Adults_Print_N170_Open_Materials'
PROJECT_DIR <- '/Users/user/Dropbox/' ## Change this to the directory in which your project folder is located

knitr::opts_knit$set(root.dir = (file.path(PROJECT_DIR, PROJECT)))

library(dplyr)
library(nnet)
```

```{r include=F}
#organize files
if (dir.exists(file.path('2_pipeline'))){
  pipeline <- file.path('2_pipeline', NAME)
} else {
  pipeline <- file.path('2_pipeline', NAME)
}

if (!dir.exists(pipeline)) {
  dir.create(pipeline)
  for (folder in c('out', 'store', 'tmp')){
    dir.create(file.path(pipeline, folder))
  }
}
```
Prepare the data

```{r prepare data}
wide.data.lines <- read.csv('/Users/user/Dropbox/Megagrant_Adults Pilot_Print.N170_Re-analysis 2020/2_pipeline/1_process data/out/Print_combined_31March_wide_shifted.csv')
wide.data.lines$X <- NULL
wide.V124.V174 <- wide.data.lines[, c(1:5, 117:192, 533)]
# remember that IDLE counts columns its own way. Always check it manually, don't rely on GUI!
wide.V124.V174$max.amp <- apply(wide.V124.V174[, 18:68], 1, max)
wide.V124.V174$max.pos.rel <- max.col(wide.V124.V174[, 18:68])
wide.V124.V174$max.pos.abs <- wide.V124.V174$max.pos.rel + 17
wide.V124.V174$start.abs <- wide.V124.V174$max.pos.abs - 12
wide.V124.V174$finish.abs <- wide.V124.V174$max.pos.abs + 12 

for (i in 1:nrow(wide.V124.V174)) {
  start = wide.V124.V174[i, 86]
  finish = wide.V124.V174[i, 87]
  wide.V124.V174$P100.mean <- apply(wide.V124.V174[,start:finish], 1, mean)
}

wide.V124.V174$P100.peak.V <- c('V0')

for (i in 1:nrow(wide.V124.V174)) {
  V.time = wide.V124.V174[i, 85]
  wide.V124.V174[i, 89] <- names(wide.V124.V174[V.time])
}

P1.dataset <- wide.V124.V174[, c(1:5, 82, 83, 88, 89)]
#write.csv(P1.dataset, "P1_dataset_io.csv", row.names=FALSE)
saveRDS(P1.dataset, file = file.path(pipeline, 'out', 'P1_dataset'))


```

```{r N170 dataset}

wide.V174.V224 <- wide.data.lines[, c(1:5, 167:242, 533)]
# remember that IDLE counts columns its own way. Always check it manually, don't rely on GUI!
wide.V174.V224$min.amp <- apply(wide.V174.V224[, 18:68], 1, min)
wide.V174.V224$min.pos.rel <- max.col(-(wide.V174.V224[, 18:68])) # minus means that it takes from multiplied by -1 matrix (which gives us min)
wide.V174.V224$min.pos.abs <- wide.V174.V224$min.pos.rel + 17
wide.V174.V224$start.abs <- wide.V174.V224$min.pos.abs - 12
wide.V174.V224$finish.abs <- wide.V174.V224$min.pos.abs + 12 

for (i in 1:nrow(wide.V174.V224)) {
  start = wide.V174.V224[i, 86]
  finish = wide.V174.V224[i, 87]
  wide.V174.V224$N170.mean <- apply(wide.V174.V224[,start:finish], 1, mean)
}

wide.V174.V224$N170.peak.V <- c('V0')

for (i in 1:nrow(wide.V174.V224)) {
  V.time = wide.V174.V224[i, 85]
  wide.V174.V224[i, 89] <- names(wide.V174.V224[V.time])
}

N170.dataset <- wide.V174.V224[, c(1:5, 82, 83, 88, 89)]
#write.csv(N170.dataset, "N170_dataset_io.csv", row.names=FALSE)

saveRDS(N170.dataset, file = file.path(pipeline, 'out', 'N170_dataset'))
```

